/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.main;

import app.game.GlobalStatics;
import app.net.client.ClientApp;
import app.net.server.ServerApp;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import socket.chat.ChatClientGUI;
import socket.chat.ChatServer;

/**
 *
 * @author Spyros
 */
public class GameMain {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int choice = JOptionPane.showOptionDialog(null,
                "Host a game or join an existing one?",
                "Msc Fighter Game",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null,
                new Object[]{"Host", "Join"},
                null);

        if (choice == JOptionPane.YES_OPTION) {
            JFrame jFrame = new JFrame();
            jFrame.setSize(150, 100);
            jFrame.setVisible(true);
            Button stopButton = new Button("stop");
            final ChatServer cs = new ChatServer();
            Thread t1 = new Thread() {
                /*
                 * Initialize the chat Server
                 */

                @Override
                public void run() {
                    cs.init();
                }
            };
            final ServerApp app = new ServerApp();
            Thread t2 = new Thread() {
                /*
                 * Initialize the game Server
                 */

                @Override
                public void run() {
                    app.init();
                    app.start();
                }
            };
            t1.start();
            t2.start();
            stopButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("stop?");
//                    cs.stop();
                    app.stop();
                    System.exit(0);
                }
            });
            jFrame.add(stopButton);
            jFrame.addWindowListener(new WindowAdapter() {

                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });

        } else if (choice == JOptionPane.NO_OPTION) {
            final String ip = JOptionPane.showInputDialog("Host IP:", "127.0.0.1");
            Thread t1 = new Thread() {
                /*
                 * Initialize the chat client
                 */

                @Override
                public void run() {
                    ChatClientGUI chatClientGUI = new ChatClientGUI(ip);
                    chatClientGUI.init();
                }
            };
            Thread t2 = new Thread() {
                /*
                 * Initialize the game client
                 */

                @Override
                public void run() {
                    ClientApp app = new ClientApp();
                    app.init(GlobalStatics.port, ip);
                }
            };
            t1.start();
            t2.start();

        }
    }
}
